package com.hut.bms.controller;

import com.hut.bms.model.BookInfo;
import com.hut.bms.service.InsertBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 1.添加图书
 */
@Slf4j
@RequestMapping("/BMS")
@RestController
public class InsertBookController {
    @Autowired
    private InsertBookService insertBookService;
    @RequestMapping(value = "/insertBook",produces = "application/json")
    public String insertBook(BookInfo bookInfo) {
        //打印从前端参数传入到后端的日志
        log.info("添加图书, 接收到参数bookInfo: {}", bookInfo);
        //校验参数
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null) {
            return "输入参数不合法";
        }
        //添加图书
        try {
            Integer line = insertBookService.addBookByBookInfo(bookInfo);
            if (line > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("添加图书异常，e：",e);
        }
        return "添加失败";
    }
}
